//
//  GRSiftSortHUD.m
//  YC_GRice
//
//  Created by Feng Liu on 2018/10/7.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRSiftSortHUD.h"
#import "GRSiftSortViewCell.h"

@interface GRSiftSortHUD () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    NSInteger itemCount;
    NSInteger categoryId;
}
@property (nonatomic, strong) UIView *backgroundView;

@property (nonatomic, strong) UIView *sortHUD;

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UILabel *titleLabel;

@end


@implementation GRSiftSortHUD

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self drawUI];
    }
    return self;
}

- (void)drawUI {
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.backgroundView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    [window addSubview:self.backgroundView];
    [window bringSubviewToFront:self.backgroundView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideHUD)];
    [self.backgroundView addGestureRecognizer:tap];
    
    self.sortHUD = [[UIView alloc] initWithFrame:CGRectMake(KscreenWidth/2, KscreenHeight/2, 0, 0)];
    self.sortHUD.backgroundColor       = [UIColor whiteColor];
    self.sortHUD.layer.cornerRadius    = 5;
    self.sortHUD.layer.masksToBounds = YES;
    [window addSubview:self.sortHUD];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:self.sortHUD.frame];
    self.titleLabel.text       = @"筛选龟米商家类别";
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.font       = [UIFont boldSystemFontOfSize:17];
    self.titleLabel.textColor= KGMBLACK_COLOR;
    [self.sortHUD addSubview:self.titleLabel];
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:self.sortHUD.frame];
    self.pageControl.pageIndicatorTintColor           = HEXCOLOR(0xa0a0a0);
    self.pageControl.currentPageIndicatorTintColor = KGMTHEM_COLOR;
    [self.sortHUD addSubview:self.pageControl];
    
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.scrollDirection                   = UICollectionViewScrollDirectionHorizontal;
    self.collectionView = [[UICollectionView alloc] initWithFrame:self.sortHUD.frame collectionViewLayout:flowLayout];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.pagingEnabled    = YES;
    self.collectionView.delegate     = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"GRSiftSortViewCell" bundle:nil] forCellWithReuseIdentifier:@"GRSiftSortViewCell"];
    [self.sortHUD addSubview:self.collectionView];
    
    self.sortHUD.hidden           = YES;
    self.backgroundView.hidden = YES;
}

- (void)hideHUD {
    WEAKSELF
    [UIView animateWithDuration:.3 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        weakSelf.backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        weakSelf.sortHUD.frame                         = CGRectMake(KscreenWidth/2, KscreenHeight/2, 0, 0);
        weakSelf.titleLabel.frame                        = CGRectMake(KscreenWidth/2, KscreenHeight/2, 0, 0);
        weakSelf.collectionView.frame                  = CGRectMake(KscreenWidth/2, KscreenHeight/2, 0, 0);
        weakSelf.pageControl.frame                     = CGRectMake(KscreenWidth/2, KscreenHeight/2, 0, 0);
    } completion:^(BOOL finished) {
        weakSelf.sortHUD.hidden           = YES;
        weakSelf.backgroundView.hidden = YES;
    }];
}

-(void)showHUD {
    self.sortHUD.hidden           = NO;
    self.backgroundView.hidden = NO;
    WEAKSELF
    [UIView animateWithDuration:.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        weakSelf.backgroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        weakSelf.sortHUD.frame                          = CGRectMake(10, KscreenHeight/2-(KscreenWidth-20)/4-45, KscreenWidth-20, (KscreenWidth-20)/2 + 90);
        weakSelf.titleLabel.frame                         = CGRectMake(0, 0, KscreenWidth-20, 45);
        weakSelf.collectionView.frame                   = CGRectMake(0, 45, KscreenWidth-20, (KscreenWidth-20)/2);
        weakSelf.pageControl.frame                      = CGRectMake(0, 45 + (KscreenWidth-20)/2, KscreenWidth-20, 45);
    } completion:nil];
}

-(void)setClassList:(NSArray *)classList {
    _classList = classList;
    itemCount = (classList.count / 8 + (classList.count % 8 > 0 ? 1 : 0)) * 8;
    if (itemCount/8 > 1) {
        self.pageControl.numberOfPages = itemCount / 8;
    }
    [self.collectionView reloadData];
}

#pragma MARK ----------- UICollectionView代理 数据源 -------------
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return itemCount;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GRSiftSortViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GRSiftSortViewCell" forIndexPath:indexPath];
    if (indexPath.item < self.classList.count) {
        GRStoreClassModel *model = self.classList[indexPath.item];
        cell.classModel  = model;
        cell.hideOption  = NO;
        cell.selectIMG.hidden = (model.id == categoryId ? NO : YES);
    }else {
        cell.hideOption  = YES;
        cell.selectIMG.hidden = YES;
    }
    return cell;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((KscreenWidth-20)/4, (KscreenWidth-20)/4);
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.f;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.f;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.item < self.classList.count) {
        [self hideHUD];
        
        GRStoreClassModel *model     = self.classList[indexPath.item];
        categoryId                           = model.id;
        [collectionView reloadData];
        
        [GRHUD showLoading:@"正在加载"];
        if ([self.delegate respondsToSelector:@selector(didRefreshNearbyShopWithClass:)]) {
            [self.delegate didRefreshNearbyShopWithClass:model.id];
            [GRHUD dismissHUD];
        }
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageControl.currentPage = scrollView.contentOffset.x / self.collectionView.width;
}


@end
